//
//  Team9WorkReviewVIew.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/17.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9WorkReviewVIew.h"
#import "Team9JQHeader.h"
#import "Team9Header.h"
#import "Team9WorkReviewsModel.h"

@interface Team9WorkReviewVIew ()
@property(nonatomic, strong) Team9BaseLabel *titlelabel;
@property(nonatomic, strong) Team9BaseLabel *nickLabel;
@property(nonatomic, strong) UIImageView *faceiImageVew;
@property(nonatomic, strong) Team9BaseLabel *contentlabel;
@property(nonatomic, strong) NSMutableArray<UIImageView *> *starImageViews1;
@property(nonatomic, strong) NSMutableArray<UIImageView *> *starImageViews2;
@property(nonatomic, strong) NSMutableArray<UIImageView *> *starImageViews3;
@property(nonatomic, strong) NSMutableArray<UIImageView *> *starImageViews4;
@property(nonatomic ,strong) UIButton *button;
@end
@implementation Team9WorkReviewVIew

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)test{
    self.titlelabel.text = @"15042341680";
    self.faceiImageVew.image = [UIImage imageNamed:@"111"];
    self.nickLabel.text = @"123";
}
- (void)createView{
    self.titlelabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_titlelabel];
    
    self.nickLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_nickLabel];
    
    self.faceiImageVew = [UIImageView new];
    [self addSubview:_faceiImageVew];
    
    self.contentlabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_contentlabel];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_button];
    
    self.starImageViews1 = @[].mutableCopy;
    for (int i = 0; i < 5; i++) {
        UIImageView *starImageView = [UIImageView new];
        [self.starImageViews1 addObject:starImageView];
        [self addSubview:starImageView];
        starImageView.image = [UIImage imageNamed:@"halfStar"];
    }
    self.starImageViews2 = @[].mutableCopy;
    for (int i = 0; i < 5; i++) {
        UIImageView *starImageView = [UIImageView new];
        [self.starImageViews2 addObject:starImageView];
        [self addSubview:starImageView];
        starImageView.image = [UIImage imageNamed:@"halfStar"];
    }
    self.starImageViews3 = @[].mutableCopy;
    for (int i = 0; i < 5; i++) {
        UIImageView *starImageView = [UIImageView new];
        [self.starImageViews3 addObject:starImageView];
        [self addSubview:starImageView];
        starImageView.image = [UIImage imageNamed:@"halfStar"];
    }
    self.starImageViews4 = @[].mutableCopy;
    for (int i = 0; i < 5; i++) {
        UIImageView *starImageView = [UIImageView new];
        [self.starImageViews4 addObject:starImageView];
        [self addSubview:starImageView];
        starImageView.image = [UIImage imageNamed:@"halfStar"];
    }
    
    self.titlelabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    self.nickLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    self.contentlabel.font = [UIFont systemFontOfSize:12 * WIDTH / 375];
    self.button.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTH / 375];
    self.faceiImageVew.layer.cornerRadius = 15 * WIDTH / 375;
    self.faceiImageVew.layer.masksToBounds = YES;
    [self.button setTitle:@"查看全部信息" forState:UIControlStateNormal];
    
    [self addSubview:_button];
    self.button.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    UIColor *color;
    if ([Team9EveningType shareEvening].eveningFailling) {
        color = [UIColor grayColor];
    } else color = [UIColor orangeColor];
    self.button.layer.borderColor = color.CGColor;
    [self.button setTitleColor:color forState:UIControlStateNormal];
    self.button.layer.borderWidth = 1.0f;
    self.button.layer.cornerRadius = 5.0f;
    [self.button addTarget:self action:@selector(detail:) forControlEvents:UIControlEventTouchUpInside];
    [self p_contraints];
}
- (void)detail:(id)sender{
    if (_block) {
        self.block();
    }
}
- (void)p_contraints{
    [self.titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.centerX.equalTo(self);
    }];
    [self.faceiImageVew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titlelabel.mas_bottom).offset(10);
        make.left.equalTo(self).offset(10);
        make.width.height.equalTo(@(30 * WIDTH / 375));
    }];
    [self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.faceiImageVew);
        make.left.equalTo(self.faceiImageVew.mas_right).offset(10);
    }];
    
    Team9BaseLabel *label1 = [[Team9BaseLabel alloc] init];
    [self addSubview:label1];
    
    Team9BaseLabel *label2 = [[Team9BaseLabel alloc] init];
    [self addSubview:label2];
    
    Team9BaseLabel *label3 = [[Team9BaseLabel alloc] init];
    [self addSubview:label3];
    
    Team9BaseLabel *label4 = [[Team9BaseLabel alloc] init];
    [self addSubview:label4];
    
    label1.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    label2.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    label3.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    label4.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    
    label1.text = @"服务态度";
    label2.text = @"施工质量";
    label3.text = @"按时完成";
    label4.text = @"报价相符";
    
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faceiImageVew.mas_bottom).offset(10);
        make.left.equalTo(self.faceiImageVew);
    }];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(@(WIDTH / 5 * 3));
        make.top.equalTo(label1);
    }];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label1.mas_bottom).offset(10);
        make.left.equalTo(self.faceiImageVew);
    }];
    [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label3);
        make.left.equalTo(label2);
    }];
    
    self.contentlabel.numberOfLines = 0;
    [self.contentlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label3.mas_bottom).offset(10);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
    }];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_contentlabel.mas_bottom).offset(10);
        make.centerX.equalTo(self);
        make.width.mas_equalTo(self.button.mas_height).multipliedBy(3.5);
    }];
    [self array:_starImageViews1 mas_contrainstsLeft:label1];
    [self array:_starImageViews2 mas_contrainstsLeft:label2];
    [self array:_starImageViews3 mas_contrainstsLeft:label3];
    [self array:_starImageViews4 mas_contrainstsLeft:label4];
    
}
- (void)setModel:(Team9WorkReviewsModel *)model
{
    if (_model != model) {
        _model = model;
    }
//    NSLog(@"%@", model.rating1);
    self.titlelabel.text = [NSString stringWithFormat:@"%d条评论", self.reviewcount.intValue];
    self.contentlabel.text = self.model.content;
    [self array:_starImageViews1 makeRatings:self.model.rating1.floatValue];
    [self array:_starImageViews2 makeRatings:self.model.rating2.floatValue];
    [self array:_starImageViews3 makeRatings:self.model.rating3.floatValue];
    [self array:_starImageViews4 makeRatings:self.model.rating4.floatValue];
    [Team9Tool sd_imageView:self.faceiImageVew image:self.model.user_face placeholderImage:@"333"];
    self.nickLabel.text = self.model.user_nick;
}

- (void)array:(NSArray<UIImageView *> *)array
mas_contrainstsLeft:(UIView *)view{
    UIImageView *lastStarView = nil;
    for (UIImageView *imageView in array) {
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_top);
            if (lastStarView) {
                make.left.equalTo(lastStarView.mas_right).offset(1);
            } else make.left.equalTo(view.mas_right).offset(10);
            make.width.equalTo(@(10 * WIDTH / 375));
            make.height.equalTo(imageView.mas_width);
        }];
        lastStarView = imageView;
    }
}
- (void)array:(NSArray<UIImageView *> *)array
  makeRatings:(CGFloat)ratings{
    NSInteger wholeStar = (NSInteger)ratings;
    BOOL halfStar = (ratings - wholeStar) >= 0.5f;
    for (NSInteger i = 0; i < 5; i++) {
        UIImageView *starImageView = array[i];
        starImageView.image = [UIImage imageNamed:@"emptyStar"];
        if (i < wholeStar) {
            starImageView.image = [UIImage imageNamed:@"oneStar"];
        }
        if (i == wholeStar && halfStar) {
            starImageView.image = [UIImage imageNamed:@"halfStar"];
        }
    }
}
@end
